import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;


import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class PhoneWeekLayout {
	
	private PhoneLayout phone;	// incorporates the standard phone interface
	private JPanel cards;
	
	List<Appointment> appointments;
	
	public PhoneWeekLayout(PhoneLayout phone){
		this.phone = phone;
		cards = phone.middleCards;
		
		
		Calendar current = Calendar.getInstance();
		
		int currentD = current.get(Calendar.DAY_OF_MONTH);
		int currentM = current.get(Calendar.MONTH) +1; 		// jan is randomly 0..
		int currentY = current.get(Calendar.YEAR);
		
		String[] stringDays = new String[] {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
		HashMap<String, Integer> mapDays = new HashMap<String, Integer>(7);			// yeah, i made a hashmap
		mapDays.put("MONDAY", 0);
		mapDays.put("TUESDAY", 1);
		mapDays.put("WEDNESDAY", 2);
		mapDays.put("THURSDAY", 3);
		mapDays.put("FRIDAY", 4);
		mapDays.put("SATURDAY", 5);
		mapDays.put("SUNDAY", 6);
		
		int diff = mapDays.get( CalendarDate.getDay(currentD, currentM, currentY) );
		int mondaytest = currentD;
		
		/* Some pretty shocking code here sorry boys! it will only ever show the week of 
		 * the current day for simplicity and will work up until the end of December as
		 * it is hard coded to work round the week in both nov/dec for demo purposes.
		 */
		switch(diff) {
			case 0: 
				break;
			case 1:
				mondaytest -= 1; break;
			case 2:
				mondaytest -= 2; break;
			case 3: 
				mondaytest -= 3; break;
			case 4:
				mondaytest -= 4; break;
			case 5:
				mondaytest -= 5; break;
			case 6:
				mondaytest -=6; break;
		}
		
		int startM = currentM;
		if (mondaytest == -2 ) {
			mondaytest = 28;
			startM = currentM-1;
		}
		
		CalendarDate start = new CalendarDate(mondaytest, startM, currentY);
		//gets the end of the week
		int nextMonday = mondaytest + 6;
		int nextMonth = startM;
		if (nextMonday > phone.monthValues[startM]) {
			nextMonday = nextMonday % phone.monthValues[startM];
			nextMonth += 1;			
		}
			
			
	 	CalendarDate end = new CalendarDate(nextMonday, nextMonth, currentY);
	 		  
	 	appointments = phone.cal.getAppointmentsBetweenDates(start, end);

	 	JPanel weekView = new JPanel(); 
		
		weekView.setBackground(Color.white);
		weekView.setPreferredSize(new Dimension(480,640));
		

		//Set up column names for the table, its a bit complex to work out dates of weeks spanning 2 months
		String[] columnNames = new String[8];
		columnNames[0] = "";
		for ( int i = 1; i < 8; i++, mondaytest++ ) {
			mondaytest = mondaytest % phone.monthValues[startM];
			if (mondaytest == 0)
				mondaytest = phone.monthValues[startM];
			columnNames[i] = ( stringDays[i-1] + " " + mondaytest );		//only accurate for 30 day months lol
		}
		
		//set up data cells for table
		Object[][] data = new Object[24][8];
		
		for (int row = 0; row < 24; row++) {
			data[row][0] = (row + ":00");
		}

		//shows appointments on table, time not really accurate
	 	for (Appointment ap : appointments) {
	 		int day = ap.date.day;
	 		int row, col;
	 		for ( int i = 1; i<8; i++ ) {
	 			if ( (start.day+i-1)%30 == day) {
	 				col = i;
	 				for (row = 0; row < 24; row++) {
	 					if ( PhoneDayLayout.appointmentHour( row, ap.start_time, ap.end_time ) ) {
	 				          data[ row ][ col ] = ap.description;
	 					}	
	 				}
	 			}
	 		}
	 	}
		
		JTable table = new JTable( data, columnNames);
		table.setRowHeight(40);
		table.setEnabled(false);
		table.setDefaultRenderer(Object.class, new TableRenderer(data));
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setPreferredSize(new Dimension(480, 640));
		table.setFillsViewportHeight(true);
		

		weekView.add(scrollPane);
		cards.add(weekView, "Week");
	}
	
	 public static String hourString( int hour ) {
		 if ( hour < 10 ) {
			 return ( "0" + hour + ":00" );
		  }	
		
		 return ( hour + ":00" );
	 }
	
	
	public class TableRenderer extends DefaultTableCellRenderer {
		
	    /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		
		private Object[][] data;
	
	    public TableRenderer( Object[][] data ) {
	      this.data = data;
	    }
	
	    public Component getTableCellRendererComponent( JTable table, Object value, boolean isSelected, boolean f, int row, int column ) {
	      Component c = super.getTableCellRendererComponent( table, value, isSelected, f, row, column );
	
	      if ( this.data[ row ][ column ] != null && column != 0) {
	        c.setBackground( Color.pink );
	      }
	      else {
	        c.setBackground( Color.white );
	      }
	      return ( c );
	    }
	  }
}

